package com.dreamchaser.familydevice.support;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 请求过滤
 * 重新包装request
 */
@Slf4j
public class HttpServletRequestReplacedFilter implements Filter {
    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        // dofilter request包装
        if (request instanceof HttpServletRequest) {
            requestWrapper = new XssSqlHttpServletRequestWrapper((HttpServletRequest) request);
        }
        //request.setAttribute("currentUser", new Account());
        //获取请求中的流放入内存，每次获取流直接冲内存中获取
        // 在chain.doFiler方法中传递新的request对象
        if (requestWrapper == null) {
            chain.doFilter(request, response);
        } else {
            log.info("dofilter request包装！");
            chain.doFilter(requestWrapper, response);
        }
    }


    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}
